package com.ljh.aiplatform.scoring;
import java.util.Date;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ljh.aiplatform.model.dto.question.QuestionContentDTO;
import com.ljh.aiplatform.model.dto.question.QuestionContentOption;
import com.ljh.aiplatform.model.entity.App;
import com.ljh.aiplatform.model.entity.Question;
import com.ljh.aiplatform.model.entity.ScoringResult;
import com.ljh.aiplatform.model.entity.UserAnswer;
import com.ljh.aiplatform.service.QuestionService;
import com.ljh.aiplatform.service.ScoringResultService;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 自定义角色测评策略
 */
@ScoringStrategyConfig(appType = 1,scoringStrategy = 0)
public class CustomTestScoringStrategy implements ScoringStrategy{

    @Resource
    private QuestionService questionService;

    @Resource
    private ScoringResultService scoringResultService;

    @Override
    public UserAnswer doScoring(List<String> choices, App app) {
        //1: 根据appid查询题目(题目表）和题目信息（题目结果表）
        Long appId = app.getId();
        LambdaQueryWrapper<Question> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.eq(Question::getAppId,appId);
        Question question = questionService.getOne(lambdaQueryWrapper1);
        LambdaQueryWrapper<ScoringResult> lambdaQueryWrapper2 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper2.eq(ScoringResult::getAppId,appId);
        List<ScoringResult> scoringResultList = scoringResultService.list(lambdaQueryWrapper2);
        //2：统计用户每个选项对应的属性个数 如 I = 10，E = 5
        Map<String,Integer> optionCount = new HashMap<>();
        //2.1取出题目列表
        final String questionContent = question.getQuestionContent();
        List<QuestionContentDTO> questionContentDTOS = JSONUtil.toList(questionContent, QuestionContentDTO.class);
        //2.2 遍历用户选项，统计每个选项对应的属性个数
        for (String choice : choices) {//遍历每个选项
            for (QuestionContentDTO questionContentDTO : questionContentDTOS) {//遍历每个题目
                String result = new String();
                for (QuestionContentOption questionContentOption : questionContentDTO.getOptions()) {//遍历题目的每个选项
                    if(choice.equals(questionContentOption.getKey())){
                        result = questionContentOption.getResult();
                        break;
                    }
                }
                optionCount.put(result,optionCount.getOrDefault(result,0)+1);
            }
        }
        //3：遍历每种评分结果 ， 计算哪个结果的得分更高
        int maxscore = 0;
        ScoringResult maxScoringResult = scoringResultList.get(0);//设置一个初始值
        for (ScoringResult scoringResult : scoringResultList) {
            final List<String> listresultProp = JSONUtil.toList(scoringResult.getResultProp(), String.class);
            final int score = listresultProp.stream().mapToInt(prop -> optionCount.getOrDefault(prop, 0)).sum();
            if(maxscore < score){
                maxscore = score;
                maxScoringResult = scoringResult;
            }
        }
        //4：构造返回值，填充答案对象的属性
        UserAnswer userAnswer = new UserAnswer();
        userAnswer.setAppId(appId);
        userAnswer.setAppType(app.getAppType());
        userAnswer.setChoices(JSONUtil.toJsonStr(choices));
        userAnswer.setResultId(maxScoringResult.getId());
        userAnswer.setResultName(maxScoringResult.getResultName());
        userAnswer.setResultDesc(maxScoringResult.getResultDesc());
        userAnswer.setResultPicture(maxScoringResult.getResultPicture());
        return userAnswer;
    }
}
